package com.dingxiaopang.controller;

import com.alibaba.fastjson.JSON;
import com.dingxiaopang.domain.Order;
import com.dingxiaopang.domain.Product;
import com.dingxiaopang.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Random;

@RestController
@RequestMapping("/order/Prod")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/{pid}")
    public Order order(@PathVariable("pid") Integer pid){
        //查询商品
        log.info("接收到{}号商品的下单信息,接下来调用商品微服务来查询此商品信息");
        Product product = restTemplate.getForObject("http://localhost:8081/product/" + pid, Product.class);
        log.info("查询到{}号商品的信息,内容是:{}",pid,JSON.toJSONString(product));
        //创建订单
        Order order = new Order();
        order.setUid(1);
        order.setUsername("测试用户");
        order.setPid(pid);
        order.setPprice(product.getPprice());
        order.setNumber(10);
        order.setPname(product.getPname());
        orderService.createOrder(order);
        log.info("创建订单成功,订单信息为{}",JSON.toJSONString(order));
        return order;
    }
}
